package com.zc.operator.service.impl;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.zc.operator.mapper.ChargeStationPriceTacticsMapper;
import com.zc.operator.domain.ChargeStationPriceTactics;
import com.zc.operator.service.ChargeStationPriceTacticsService;

@Service
public class ChargeStationPriceTacticsServiceImpl implements ChargeStationPriceTacticsService {

    @Resource
    private ChargeStationPriceTacticsMapper chargeStationPriceTacticsMapper;

    @Override
    public int insert(ChargeStationPriceTactics record) {
        return chargeStationPriceTacticsMapper.insert(record);
    }

    @Override
    public int insertSelective(ChargeStationPriceTactics chargeStationPriceTactics) {
        Long defaultId = chargeStationPriceTacticsMapper.hasDefault(chargeStationPriceTactics.getStationCode());
        if (defaultId == null) {
            chargeStationPriceTactics.setIsDefault("1");
            chargeStationPriceTactics.setStatus("1");
        } else {
            List<Long> idList = chargeStationPriceTacticsMapper.checkDate(chargeStationPriceTactics.getStartTime(), chargeStationPriceTactics.getEndTime(), chargeStationPriceTactics.getStationCode(), chargeStationPriceTactics.getId());
            if (!idList.isEmpty()) {
                throw new RuntimeException("生效时间不能重复");
            }
        }
        return chargeStationPriceTacticsMapper.insertSelective(chargeStationPriceTactics);
    }

    @Override
    public int insertList(List<ChargeStationPriceTactics> list) {
        return chargeStationPriceTacticsMapper.insertList(list);
    }

    @Override
    public List<ChargeStationPriceTactics> selectByAll(ChargeStationPriceTactics chargeStationPriceTactics) {
        return chargeStationPriceTacticsMapper.selectByAll(chargeStationPriceTactics);
    }

    @Override
    public ChargeStationPriceTactics selectById(Long id) {
        return chargeStationPriceTacticsMapper.selectById(id);
    }

    @Override
    public int modifyById(ChargeStationPriceTactics updated) {
        List<Long> idList = chargeStationPriceTacticsMapper.checkDate(updated.getStartTime(), updated.getEndTime(), updated.getStationCode(), updated.getId());
        if (!idList.isEmpty()) {
            throw new RuntimeException("生效时间不能重复");
        }

        return chargeStationPriceTacticsMapper.modifyById(updated);
    }

    @Override
    public int deleteById(Long id) {
        return chargeStationPriceTacticsMapper.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> getList(ChargeStationPriceTactics chargeStationPriceTactics) {
        return chargeStationPriceTacticsMapper.getList(chargeStationPriceTactics);
    }

    @Override
    public ChargeStationPriceTactics getStationNowTactics(ChargeStationPriceTactics chargeStationPriceTactics) {
        //获取当前时间段策略（非默认且生效）
        ChargeStationPriceTactics stationNowTactics = chargeStationPriceTacticsMapper.getStationNowTactics(chargeStationPriceTactics);
        if (Objects.isNull(stationNowTactics)) {
            //查询默认策略
            chargeStationPriceTactics.setIsDefault("1");
            stationNowTactics = chargeStationPriceTacticsMapper.getStationNowTactics(chargeStationPriceTactics);
            //没有默认策略，返回空
            return stationNowTactics;
        } else {
            return stationNowTactics;
        }
    }

}
